﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class frmManageRequest : System.Web.UI.Page
    {
        LogicUniversityT2Entities ct = new LogicUniversityT2Entities();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InventoryUser aaa = (InventoryUser)Session["username"];

                refreshEmpReqList();
            }
        }

        protected void btnApproveAll_Click(object sender, EventArgs e)
        {

            foreach (GridViewRow gr in gvManageRequest.Rows)
            {
                RadioButton rb1 = (RadioButton)gr.Cells[4].FindControl("rbApprove");
                rb1.Checked = true;

                if (gr != null)
                {
                    LinkButton lb = (LinkButton)gr.Cells[0].FindControl("LinkButton1");

                    string empReqID = lb.Text;

                    EmployeeRequisition select = ManageRequestManager.selectedEmpRequisition(empReqID);
                    DepartmentRequisition dr = new DepartmentRequisition();

                    select.Status = "approve";

                    ct.SaveChanges();

                    ManageRequestManager.saveToDepartmentRequisitionNDetail(select);

                    var aa = from erd in ct.EmployeeRequisitionDetails
                             join er in ct.EmployeeRequisitions on erd.EmployeeRequisitionID equals er.EmployeeRequisitionID
                             where er.EmployeeRequisitionID == empReqID

                             select new
                             {

                                 itemId = erd.ItemID,
                                 deptId = er.DepartmentID
                             };
                    var itemlist = aa.ToList();
                }
            }
            refreshEmpReqList();
        }
        public void refreshEmpReqList()
        {
            lblNumber.Text = ManageRequestManager.getNoOfRequest().ToString();
            if (Convert.ToInt32(lblNumber.Text) == 0)
            {
                btnApproveAll.Visible = false;
                btnOK.Visible = false;
            }
            gvManageRequest.DataSource = ManageRequestManager.getEmployeeRequisitionList();
            gvManageRequest.DataBind();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            foreach (GridViewRow gr in gvManageRequest.Rows)
            {

                if (gr != null)
                {
                    RadioButton rb1 = (RadioButton)gr.Cells[4].FindControl("rbApprove");
                    RadioButton rb2 = (RadioButton)gr.Cells[4].FindControl("rbReject");
                    LinkButton lb = (LinkButton)gr.Cells[0].FindControl("LinkButton1");

                    string empReqID = lb.Text;

                    EmployeeRequisition select = ManageRequestManager.selectedEmpRequisition(empReqID);
                    DepartmentRequisition dr = new DepartmentRequisition();

                    if (rb1.Checked == true)
                    {

                        select.Status = "approve";

                        ct.SaveChanges();

                        ManageRequestManager.saveToDepartmentRequisitionNDetail(select);

                        var aa = from erd in ct.EmployeeRequisitionDetails
                                 join er in ct.EmployeeRequisitions on erd.EmployeeRequisitionID equals er.EmployeeRequisitionID
                                 where er.EmployeeRequisitionID == empReqID

                                 select new
                                 {

                                     itemId = erd.ItemID,
                                     deptId = er.DepartmentID
                                 };
                        var itemlist = aa.ToList();

                    }

                    if (rb2.Checked == true)
                    {

                        select.Status = "reject";

                        ct.SaveChanges();
                    }

                }
            }
            refreshEmpReqList();
        }
        protected void gvManageRequest_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "goToDetailPage")
            {
                LinkButton btndetails = (LinkButton)e.CommandSource;

                Response.Redirect("frmManageRequestDetail.aspx?ID=" + btndetails.Text);

            }
        }

        protected void gvManageRequest_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}
